﻿using System.Security.Claims;
using Volo.Abp.Authorization.Permissions;

namespace Ebusiness_Authservice.Permission
{
    public class CustomPermissionValueProvider : PermissionValueProvider
    {
        public CustomPermissionValueProvider(IPermissionStore permissionStore) : base(permissionStore)
        {

        }

        public const string ProviderName = "Custom";

        /// <summary>
        /// 基于用户名鉴权
        /// </summary>
        public override string Name => "Custom";

        public IHttpContextAccessor HttpContext { get; set; }

        public override async Task<PermissionGrantResult> CheckAsync(PermissionValueCheckContext context)
        {
            #region 持久化鉴权
            {
                //throw new NotImplementedException();
                //if (HttpContext.HttpContext.User.Identity?.IsAuthenticated == false)
                //    return PermissionGrantResult.Undefined;
                ////获取Cookie中的用户
                //ClaimsPrincipal claimsPrincipal = context.Principal;
                ////获取Product_Select权限
                //Claim claim = claimsPrincipal.Claims.Where(x => x.Type == JwtRegisteredClaimNames.Name).First();
                //List<PermissionGrants> permissionGrants = await PermissionGrants.GetListAsync();
                //var userList = permissionGrants.Where(x => x.ProviderName == Name && x.ProviderKey == claim.Value).ToList();
                ////获取接口上的权限
                //string PermissionName = context.Permission.Name;
                ////if (PermissionName == "Product_Select_Page")
                ////    return PermissionGrantResult.Undefined;
                ////鉴定用户是否有权限
                //if (userList.Any(x => x.PermissionName == PermissionName))
                //{
                //    //授予权限
                //    //return PermissionGrantResult.Prohibited;
                //    return PermissionGrantResult.Granted;
                //    //return PermissionGrantResult.Prohibited;
                //}
                //else
                //{
                //    //鉴权失败
                //    return PermissionGrantResult.Undefined;
                //}

                //return PermissionGrantResult.Undefined;

                return PermissionGrantResult.Granted;
            }
            #endregion
        }

        public override Task<MultiplePermissionGrantResult> CheckAsync(PermissionValuesCheckContext context)
        {
            return Task.FromResult(new MultiplePermissionGrantResult());
        }
    }
}
